/* 
 * Definition of network interfaces configuration file path
 * This file contains network interface configurations on Linux systems
 * Located in the /etc/network directory and controls network setup
 */
#define INTERFACES_FILE "/etc/network/interfaces"

/* 
 * Maximum number of routes that can be stored in the RouteList structure
 * Defines the upper limit for route entries to prevent buffer overflow
 */
#define MAX_ROUTES 10

/* 
 * Maximum length for network interface name string
 * Interface names are typically short like "eth0", "wlan0", "ppp0"
 */
#define MAX_IFACE_LEN 16

/* 
 * Maximum length for gateway IP address string
 * Accommodates IPv4 addresses (15 chars) and IPv6 addresses (39 chars)
 */
#define MAX_GATEWAY_LEN 32

/* 
 * Structure definition for individual route information
 * Contains all parameters needed to define a network routing entry
 */
typedef struct {
    /* 
     * Gateway IP address for the route
     * Specifies the next hop router for network packets
     * Stored as string to accommodate both IPv4 and IPv6 formats
     */
    char gateway[MAX_GATEWAY_LEN];
    /* 
     * Network interface name associated with this route
     * Identifies which physical/virtual interface the route uses
     * Examples: "eth0", "wlan0", "ppp0"
     */
    char iface[MAX_IFACE_LEN];
    /* 
     * Route metric value for route priority determination
     * Lower metric values indicate preferred routes
     * Used when multiple routes to same destination exist
     */
    int metric;
} RouteInfo;

/* 
 * Structure definition for a collection of routes
 * Provides container for managing multiple routing entries
 */
typedef struct {
    /* 
     * Array of route information structures
     * Stores up to MAX_ROUTES individual route entries
     * Provides fixed-size storage for route management
     */
    RouteInfo routes[MAX_ROUTES];
    /* 
     * Current number of valid routes in the routes array
     * Tracks how many route entries are actually populated
     * Must be between 0 and MAX_ROUTES inclusive
     */
    int count;
} RouteList;

/* 
 * Function prototype to retrieve all default routes from system
 * Queries system routing table and returns default gateway routes
 * Returns RouteList structure containing discovered default routes
 */
RouteList get_default_routes() ;

/* 
 * Function prototype to check if a specific route exists in interfaces file
 * Searches the network interfaces configuration file for route entry
 * Returns 1 if route exists, 0 if not found, negative for errors
 */
int route_exists(const char *dev, const char *dst, const char *mask) ;

/* 
 * Function prototype to add a new route to interfaces configuration file
 * Appends route configuration to /etc/network/interfaces file
 * Returns 0 on success, non-zero error code on failure
 */
int add_route_to_file(const char *dev, const char *dst, const char *mask) ;

/* 
 * Function prototype to remove a route from interfaces configuration file
 * Searches for and deletes specified route entry from configuration
 * No return value as it's designed to complete operation silently
 */
void delete_route_from_file(const char *dev, const char *dst, const char *mask) ;

/* 
 * Function prototype to execute system shell commands
 * Provides wrapper for system() function with error handling
 * Returns command exit status or error code
 */
int execute_command(const char *command) ;

/* 
 * Function prototype to add a route to system routing table
 * Uses system commands like 'ip route add' or 'route add'
 * Returns 0 on success, non-zero error code on failure
 */
int add_route(const char *dev, const char *dst, const char *mask);

/* 
 * Function prototype to delete a route from system routing table
 * Uses system commands like 'ip route del' or 'route del'
 * Returns 0 on success, non-zero error code on failure
 */
int del_route(const char *dev, const char *dst, const char *mask);

/* 
 * Function prototype to modify an existing route in system
 * Implements route modification by deleting old and adding new route
 * Provides atomic-like operation for route updates
 */
int mod_route(const char *dev, const char *dst, const char *mask) ;

/* 
 * Function prototype to construct network configuration file path
 * Generates device-specific configuration file paths based on interface name
 * Returns 0 on success, non-zero error code if path construction fails
 */
int get_network_config_file_path(char *path, size_t size, const char *dev);

/* 
 * Function prototype to verify route existence in configuration file
 * Performs detailed search for specific route parameters in file
 * Returns 1 if exact route match found, 0 if not found
 */
int check_route_exists(const char *file_path, const char *dst, const char *prefix_length_str);

/* 
 * Function prototype to set default route for network interface
 * Configures the default gateway route for specified interface
 * Used to establish primary internet connectivity path
 */
int set_default_route(const char *dev);

/* 
 * Function prototype to add default route to interfaces file
 * Persists default gateway configuration in network interfaces file
 * Ensures route survives system reboots
 */
int set_default_route_to_file(const char *dev,const char *gateway) ;

/* 
 * Function prototype to add default route with specific metric
 * Configures default gateway with priority metric for route selection
 * Lower metric values give higher route priority
 */
int add_default_route(const char *dev, const char *gateway, const int metric) ;

/* 
 * Function prototype to remove all default routes from system
 * Clears all default gateway entries from routing table
 * Used for network reconfiguration or troubleshooting
 */
int clear_all_default_routes() ;

/* 
 * Function prototype to create network configuration file
 * Generates custom network configuration files with specified parameters
 * Used for dynamic network profile management
 */
void create_network_file(const char *param);

/* 
 * Function prototype to create PPP network configuration
 * Specialized function for Point-to-Point Protocol configuration
 * Sets up dial-up or serial connection network files
 */
void creat_PPP_network_file(void);